package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsService wmNewsService;
    /**
     * 新增频道信息
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {

        if (!(wmChannel != null && StringUtils.isNotBlank(wmChannel.getName()) && wmChannel.getOrd()>=0)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        // 判断频道是否存在
        if(channel != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据频道条件的分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pagelist(ChannelDto dto) {

        if (dto == null ){
            log.error("传过来的doto为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        log.info("接收到的page-----{}",dto.getPage());
        log.info("接收到的size-----{}",dto.getSize());
        IPage<WmChannel> page = new Page<>(dto.getPage() ,dto.getSize());

        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();

        if (dto.getName() != null) {
            queryWrapper.like(WmChannel::getName,dto.getName());
        }

        if (dto.getStatus() != null) {
            queryWrapper.eq(WmChannel::getStatus ,dto.getStatus());
        }

        queryWrapper.orderByDesc(WmChannel::getCreatedTime);

        page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

        pageResponseResult.setData(page.getRecords());

        log.info("返回的结果是：{}",pageResponseResult);

        return pageResponseResult;
    }

    /**
     * 对channel表的修改
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel dto) {
        if ( dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (!dto.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }

        updateById(dto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 对频道的删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {

        log.info("Service 拿到的id{}",id);
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        List<WmNews> list = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));

        log.info("通过channelid 查询到的 wmnews----{}",list);
        if (list.size() <= 0) {

            removeById(id);

        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}